﻿using System;
using ZX.Web.UI.Controls;
using ZX.CRM.Bussiness;
using System.Data;

namespace ZX.WebApp.DeskTop.DeskTopItem
{
    public partial class ValuePercent : BasePage
    {
        BCrmReport BReport = new BCrmReport();   //数据业务类
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {
            try
            {
                DataSet dataSources = new DataSet();
                dataSources.DataSetName = "DsValuePercent";
                DataTable sourcePercent = new DataTable();
                DataTable sourceValue = new DataTable();
                string strDate = Request["date"];
                string startDate = DateTime.Now.AddDays(-DateTime.Now.Day + 1).ToShortDateString();
                string endDate = DateTime.Now.ToShortDateString();
                if (strDate == "month")
                {
                    startDate = DateTime.Now.AddDays(-DateTime.Now.Day + 1).ToShortDateString(); endDate = DateTime.Now.AddMonths(1).AddDays(-DateTime.Now.Day).ToShortDateString();
                }
                else if (strDate == "season")
                {
                    int season = DateTime.Now.Month % 3;
                    if (season == 0)
                    {
                        startDate = DateTime.Now.Year.ToString() + "-" + (DateTime.Now.Month - 2).ToString() + "-01"; endDate = DateTime.Now.AddMonths(1).AddDays(-DateTime.Now.Day).ToShortDateString();
                    }
                    else if (season == 1)
                    {
                        startDate = DateTime.Now.AddDays(-DateTime.Now.Day + 1).ToShortDateString(); endDate = DateTime.Now.AddMonths(3).AddDays(-DateTime.Now.Day).ToShortDateString();
                    }
                    else if (season == 2)
                    {
                        startDate = DateTime.Now.Year.ToString() + "-" + (DateTime.Now.Month - 1).ToString() + "-01"; endDate = DateTime.Now.AddMonths(2).AddDays(-DateTime.Now.Day).ToShortDateString();
                    }
                }
                else if (strDate == "year")
                {
                    startDate = DateTime.Now.Year.ToString() + "-01-01";
                    endDate = DateTime.Now.Year.ToString() + "-12-31";
                }
                else
                {
                    startDate = DateTime.Now.AddDays(-DateTime.Now.Day + 1).ToShortDateString(); endDate = DateTime.Now.AddMonths(1).AddDays(-DateTime.Now.Day).ToShortDateString();
                }
                sourcePercent = BReport.ReturnValuePercent(new Guid(this.UserId.ToString()), startDate, endDate, getPowerType);
                sourceValue = BReport.ReturnValuePercent(new Guid(this.UserId.ToString()), startDate, endDate, getPowerType);
                if (sourcePercent == null || sourcePercent.Rows.Count == 0 || sourceValue == null || sourceValue.Rows.Count == 0) 
                { 
                    lblEpm.Text = "<br><font color='red'>&nbsp;&nbsp;&nbsp;&nbsp;没有数据</font>";
                    this.lblTitle.Visible = false;
                    this.lblTotalMoney.Visible = false;
                    this.lblEpm.Visible = false;
                    return;
                }
                decimal totalMoney = 0;
                for (int i = 0; i < sourceValue.Rows.Count; i++)
                {
                    totalMoney = totalMoney + (decimal)sourceValue.Rows[i][1];
                }
                lblTotalMoney.Text = "<font color='red'>" + totalMoney.ToString() + "万</font>";
                sourcePercent.TableName = "dtValuePercent";
                sourceValue.TableName = "dtValue";
                dataSources.Tables.Add(sourcePercent.Copy());
                dataSources.Tables.Add(sourceValue.Copy());
                if (dataSources == null)
                {
                    lblEpm.Text = "<br><font color='red'>&nbsp;&nbsp;&nbsp;&nbsp;没有数据</font>";
                    this.lblTitle.Visible = false;
                    this.lblTotalMoney.Visible = false;
                    this.lblEpm.Visible = false;
                    return;
                }
                ZX.CRM.Report.CrmReport.RptValuePercent crm = new ZX.CRM.Report.CrmReport.RptValuePercent();
                crm.DataSource = dataSources;
                this.ReportViewer.Report = crm;
            }
            catch (Exception ex)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "script", "<script language='javascript'>alert('生成报表出现错误:" + ex.Message + "');</script>");
            }
        }

        private int getPowerType
        {
            get
            {
                int powertype = 0;
                if (Request["PowerType"] == null || Request["PowerType"] == "" || Request["PowerType"] == "0")
                    powertype = 0;
                else
                    powertype = 1;
                return powertype;
            }
        }
    }
}